package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.SinaFriendEntity;
import com.yksj.healthtalk.ui.R;


/**
 * @author Administrator
 *新浪微博好友适配器
 */
public class SinaWeiboFriendsAdapter extends BaseAdapter{
	private final ArrayList<SinaFriendEntity> entities;
	private final LayoutInflater inflater;
	private final ImageLoader mImageLoader;
	private final Drawable maleDrawable,femalDrawable,attenDrawable,notAttenDrawable;
	private final Context mContext;
	final Map<SinaFriendEntity,SinaFriendEntity> mCheckedMap = new LinkedHashMap<SinaFriendEntity, SinaFriendEntity>();
	
	public SinaWeiboFriendsAdapter(Context context , ArrayList<SinaFriendEntity> entities){
		this.entities = entities;
		inflater =LayoutInflater.from(context);
		mContext=context;
		mImageLoader = ImageLoader.getInstance();
	
		attenDrawable = context.getResources().getDrawable(R.drawable.attention);
		notAttenDrawable = context.getResources().getDrawable(R.drawable.not_attention);
		femalDrawable = context.getResources().getDrawable(R.drawable.women);
		maleDrawable = context.getResources().getDrawable(R.drawable.man);
	}
	@Override
	public int getCount() {
		return entities.size();
	}

	public void setAdapterData(ArrayList<SinaFriendEntity> arrayList){
		entities.clear();
		entities.addAll(arrayList);
		notifyDataSetChanged();
	}
	@Override
	public Object getItem(int position) {
		return entities.get(position);	
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public List<SinaFriendEntity> getAllSelected(){
		return new ArrayList<SinaFriendEntity>(mCheckedMap.values());
	}
	
	/**
	 * 添加索引
	 * @param index
	 */
	public void addChecked(SinaFriendEntity entity){
		mCheckedMap.put(entity,entity);
		notifyDataSetChanged();
	}
	
	/**
	 * 删除索引
	 * @param index
	 */
	public void removeChecked(SinaFriendEntity entity){
		mCheckedMap.remove(entity);
	}
	
	public void removeAllCheked(){
		mCheckedMap.clear();
		notifyDataSetChanged();
	}
	
	public void addAllCheck(){
		for (SinaFriendEntity entity : entities) {
			mCheckedMap.put(entity,entity);
		}
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null || convertView.getTag() == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.sina_weibo_friends_item, null);
			viewHolder.head = (ImageView) convertView.findViewById(R.id.head);
			viewHolder.name =  (TextView) convertView.findViewById(R.id.name);
			viewHolder.mSelectCheckBox =  (CheckBox) convertView.findViewById(R.id.cbx);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final SinaFriendEntity entity = entities.get(position);
		mImageLoader.displayImage(entity.getNormalHeadURL(), viewHolder.head);
		viewHolder.name.setText(entity.getName());
		
		viewHolder.mSelectCheckBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (viewHolder.mSelectCheckBox.isChecked()) {
					addChecked(entity);
					viewHolder.mSelectCheckBox.setChecked(true);
				}else {
					removeChecked(entity);
					viewHolder.mSelectCheckBox.setChecked(false);
				}
			}
		});
		viewHolder.mSelectCheckBox.setChecked(mCheckedMap.containsValue(entity));
		return convertView;
	}
	
	private static class ViewHolder{
		private ImageView head;
		private TextView name;
		private CheckBox mSelectCheckBox;
	}

}
